function results = wireless_queuing_models(varargin)
% 无线通信中的排队模型分析函数
% 功能：考虑信道状态变化的排队系统分析
%
% 输入参数：
%   'arrival_rates' - 到达率向量 (默认: 0.1:0.1:0.9)
%   'num_simulations' - 仿真次数 (默认: 10000)
%   'plot_results' - 是否绘制结果 (默认: true)
%
% 输出：
%   results - 包含分析结果的结构体
%
% 示例：
%   results = wireless_queuing_models('arrival_rates', 0.1:0.1:0.9, 'num_simulations', 5000);

% 参数解析
p = inputParser;
addParameter(p, 'arrival_rates', 0.1:0.1:0.9);
addParameter(p, 'num_simulations', 10000);
addParameter(p, 'plot_results', true);
parse(p, varargin{:});

arrival_rates = p.Results.arrival_rates;
num_simulations = p.Results.num_simulations;
plot_results = p.Results.plot_results;

% 添加路径
addpath('../Common');

% 获取颜色定义
colors = color_definitions();

fprintf('=== 无线通信中的排队模型分析 ===\n');

% 无线信道中的排队模型
channel_states = {'好', '中', '差'};
channel_probabilities = [0.4, 0.4, 0.2]; % 信道状态概率
service_rates_channel = [1.5, 1.0, 0.5]; % 不同信道状态的服务率

% 仿真无线信道排队系统
wireless_metrics = zeros(length(arrival_rates), 3);

for lambda_idx = 1:length(arrival_rates)
    lambda = arrival_rates(lambda_idx);
    
    % 蒙特卡洛仿真
    total_delay = 0;
    total_queue_length = 0;
    num_packets = 0;
    
    for sim = 1:num_simulations
        % 生成信道状态
        channel_state_idx = randsample(1:length(channel_states), 1, true, channel_probabilities);
        mu_wireless = service_rates_channel(channel_state_idx);
        
        % 生成到达间隔时间 (指数分布)
        inter_arrival_time = exprnd(1/lambda);
        
        % 生成服务时间 (指数分布，取决于信道状态)
        service_time = exprnd(1/mu_wireless);
        
        % 计算等待时间 (简化的M/M/1模型)
        if lambda < mu_wireless
            waiting_time = (lambda/mu_wireless) / (mu_wireless - lambda);
            queue_length = lambda * waiting_time;
        else
            waiting_time = inf;
            queue_length = inf;
        end
        
        total_delay = total_delay + waiting_time + service_time;
        total_queue_length = total_queue_length + queue_length;
        num_packets = num_packets + 1;
    end
    
    if num_packets > 0
        avg_delay = total_delay / num_packets;
        avg_queue_length = total_queue_length / num_packets;
        wireless_metrics(lambda_idx, :) = [avg_delay, avg_queue_length, lambda];
    end
end

% 绘制结果
if plot_results
    figure('Name', '无线信道排队模型', 'Position', [150, 150, 1000, 600]);
    
    subplot(1,2,1);
    plot(wireless_metrics(:, 3), wireless_metrics(:, 1), 'b-o', 'LineWidth', 2);
    grid on;
    xlabel('到达率 λ');
    ylabel('平均时延');
    title('无线信道中的平均分组时延');
    
    subplot(1,2,2);
    plot(wireless_metrics(:, 3), wireless_metrics(:, 2), 'r-s', 'LineWidth', 2);
    grid on;
    xlabel('到达率 λ');
    ylabel('平均队长');
    title('无线信道中的平均队长');
end

% 组织结果
results = struct();
results.arrival_rates = arrival_rates;
results.avg_delays = wireless_metrics(:, 1);
results.avg_queue_lengths = wireless_metrics(:, 2);
results.channel_states = channel_states;
results.channel_probabilities = channel_probabilities;
results.service_rates_channel = service_rates_channel;
results.colors = colors;

fprintf('无线通信中的排队模型分析完成\n');
end